package com.demo.config;

import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.demo.listener.MyListener;
import com.demo.lnterceptor.LoginInterceptor;
import com.demo.resolver.MyLocaleResolver;

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
	/**
	 * 国际化 方法名只能叫localeResolver
	 * 
	 * @return
	 */
	@Bean
	public LocaleResolver localeResolver() {
		return new MyLocaleResolver();
	}


/*
	@Bean
	public ServletRegistrationBean<MyServlet> testServlet() {
		// 参数2表示对应的servlet请求路径
		ServletRegistrationBean<MyServlet> bean = new ServletRegistrationBean<MyServlet>(new MyServlet(), "/login");
		return bean;
	}
*/
//	@Bean
//	public FilterRegistrationBean<MyFilter> testFilter() {
//		FilterRegistrationBean<MyFilter> bean = new FilterRegistrationBean<>();
//		bean.addUrlPatterns("/*");
//		bean.setFilter(new MyFilter());
//		return bean;
//	}

	@Bean
	public ServletListenerRegistrationBean<MyListener> testListener() {
		ServletListenerRegistrationBean<MyListener> bean = new ServletListenerRegistrationBean<MyListener>(
				new MyListener());
		return bean;
	}
	
	
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		// 设置请求路径为"/"时，跳转到home.html页面
		registry.addViewController("/").setViewName("/login");
	}
	/**
	 * 静态资源的访问
	 */
	
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/img/**").addResourceLocations("classpath:/img/");
	}
	/**
	 * 配置拦截器
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		//不拦截的路径
		String[] excluedPaths = {"/","/login","/toLogin","/webjars/**"};
		//配置拦截器
		registry.addInterceptor(new LoginInterceptor()).excludePathPatterns(excluedPaths);
	}


	
}
